//
//  showpictureViewController.m
//  XiangMuB
//
//  Created by 999999999999 on 16/8/19.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "showpictureViewController.h"
#import <UIImageView+WebCache.h>
#import "UIView+BSFrame.h"
#import <SVProgressHUD.h>
@interface showpictureViewController ()
//scroller
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
//图片
@property (nonatomic,strong)UIImageView *imgView;

@end

@implementation showpictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imgView = [[UIImageView alloc]init];
    //给图片添加监听器
    imgView.userInteractionEnabled  = YES;
    //点击图片返回
    [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backImage:)]];
    [self.scrollerView addSubview:imgView];
    self.imgView = imgView;
   
//点击进来图片的尺寸
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat pictureW= screenW;
    CGFloat pictureH = pictureW *self.model.height/self.model.width;
    if (pictureH > screenH) {//图片宽度超出屏幕宽度
        //需要滚动
        imgView.frame = CGRectMake(0, 0, pictureW, pictureH);
       self.scrollerView.contentSize = CGSizeMake(0, pictureH);
        
        
    }else {
        imgView.size = CGSizeMake(pictureW, pictureH);
        imgView.centerY = screenH *0.5;
    }
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.model.image2]];
}
//点击按钮返回
- (IBAction)backImage:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma  mark ---保存图片到相册
- (IBAction)saveImage:(id)sender {
    UIImageWriteToSavedPhotosAlbum(self.imgView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        [SVProgressHUD showSuccessWithStatus:@"保存失败"];
    }else {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}
@end
